/* piano.js -- maintained by hktv.cc */

	function hilite(id)
	{
			document.getElementById(id).style.backgroundColor = '#0066ff';
			document.getElementById(id).style.color = 'white';
			document.getElementById(id).style.fontWeight = 'bold';
			window.setTimeout(function() {
    			document.getElementById(id).style.backgroundColor = 'white';
				document.getElementById(id).style.color = 'silver';
				document.getElementById(id).style.fontWeight = 'normal';    				
			}, 500);
	}

	function blacklite(id)
	{
			document.getElementById(id).style.backgroundColor = '#ff6600';
			document.getElementById(id).style.color = 'white';
			document.getElementById(id).style.fontWeight = 'bold';
			window.setTimeout(function() {
    			document.getElementById(id).style.backgroundColor = 'black';
				document.getElementById(id).style.color = 'gray';
				document.getElementById(id).style.fontWeight = 'normal';    				
			}, 500);
	}

	function playTone(id)
	{
		try{

			x = document.getElementById(id); 
			x.currentTime = 0;
			x.play();
		} catch(z)
		{
			// silence, for now
		}

	}
	
	function setKey(e)
	{
	
		var type = e.type;
		var key;
		if(type === "click"){
			var key = e.target.id;
		}else{
		
			var key = e.which;
		}
		
		addPiano(type + "_" + key);
	
	}
	

	function playKey(type, id)
	{
	console.log("event: ");
	
	
  	console.log("type: " + type);

		switch(type){
			
			/** mouse is click **/			
			case 'click':			

				var m = id;
				// console.log("mouse: " + m);

				switch(m)
				{
				/** white keys **/			
				case 'c1key':
					document.getElementById("c1").play();
					hilite("c1key");
				break;
				case 'd1key':
					document.getElementById("d1").play();
					hilite("d1key");
				break;
				case 'e1key':
					document.getElementById("e1").play();
					hilite("e1key");
				break;
				case 'f1key':
					document.getElementById("f1").play();
					hilite("f1key");
				break;
				case 'g1key':
					document.getElementById("g1").play();
					hilite("g1key");
				break;
				case 'a1key':
					document.getElementById("a1").play();
					hilite("a1key");
				break;
				case 'b1key':
					document.getElementById("b1").play();
					hilite("b1key");
				break;
				case 'c2key':
					document.getElementById("c2").play();
					hilite("c2key");
				break;

				
				/** black keys **/
				case 'c1skey':
					document.getElementById("c1s").play();
					blacklite(m);
				break;
				case 'd1skey':
					document.getElementById("d1s").play();
					blacklite(m);
				break;
				case 'f1skey':
					document.getElementById("f1s").play();
					blacklite(m);
				break;
				case 'g1skey':
					document.getElementById("g1s").play();
					blacklite(m);
				break;
				case 'a1skey':
					document.getElementById("a1s").play();
					blacklite(m);
				break;
				default:
				break;
				}	
			break; /* end of 'click' case */

			/** keyboard is default **/
			default:
			var k = parseInt(id);

				switch(k)
				{

				/** white keys **/			
				case 97:
				case 65:
					t = "c1";
					playTone(t);
					hilite(t + "key");
				break;
				case 115:
				case 83:		
					t = "d1";
					playTone(t);
					hilite(t + "key");
				break;
				case 100:	
				case 68:		
					t = "e1";
					playTone(t);
					hilite(t + "key");
				break;
				case 102:	
				case 70:		
					t = "f1";
					playTone(t);
					hilite(t + "key");
				break;
				case 103:	
				case 71:		
					t = "g1";
					playTone(t);
					hilite(t + "key");
				break;
				case 104:	
				case 72:		
					t = "a1";
					playTone(t);
					hilite(t + "key");
				break;
				case 106:		
				case 74:		
					t = "b1";
					playTone(t);
					hilite(t + "key");
				break;
				case 107:	
				case 75:
					t = "c2";
					playTone(t);
					hilite(t + "key");
				break;

				/** black keys **/
				case 119:
				case 87:
					t = "c1s";
					playTone(t);
					blacklite(t + "key");
				break;
				case 101:
				case 69:
					t = "d1s";
					playTone(t);
					blacklite(t + "key");
				break;
				case 116:
				case 84:
					t = "f1s";
					playTone(t);
					blacklite(t + "key");	
				break;
				case 121:
				case 89:
					t = "g1s";
					playTone(t);
					blacklite(t + "key");
				break;
				case 117:
				case 85:
					t = "a1s";
					playTone(t);
					blacklite(t + "key");
				break;				
				default:
					// silence
				break;
				}
			break; /* end of default(keyboard) */
		}
	return false; // nothing else to do
	}

$(document).ready(function(){
document.getElementById("piano_kb").addEventListener("click", setKey, false);
$("body").keyup(function(e){setKey(e)}); 
});
